<template>
    <div class="comment-container">
        <h4>发表评论</h4>  
        <hr>
        <textarea placeholder="请输入你要BB的内容（120字）" maxlength="120" v-model="msg"></textarea>
        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>

        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,i) in comment" :key="item.add_time">
                <div class="cmt-title">
                    <span>第{{i}}楼</span>&nbsp;&nbsp;
                    <span>用户:{{item.user_name}}</span>&nbsp;&nbsp;
                    <span>发表时间:{{item.add_time | dataFomat}}</span>
                </div>
                <div class="cmt-body">
                    <p>{{item.content}}</p>
                </div>
            </div>
        </div>

        <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
import {Toast} from 'mint-ui';
export default {
  data(){
      return {
          pageindex:1,
          comment:[],
          msg:''
      }
  },
  created(){
      this.getComment();
  },
  methods:{
      getComment(){
          this.$http.get('api/getcomments/'+this.id+'?pageindex='+this.pageindex).then(result=>{
              if(result.body.status ===0 ){
                  console.log(result);
                  this.comment = this.comment.concat(result.body.message);
              }else{
                  Toast('获取评论失败');
              }
          })
      },
      getMore(){
          this.pageindex++;
          this.getComment();
      },
      postComment(){
          if(this.msg.trim().length === 0 ){
              return Toast('评论内容不能为空');
          }
        //   post有单个参数  第一个为提交的url
        //第二个为提交给服务器的数据对象{}
        //第三个为 提交时候，表单中的数据格式{emulateJSON:true}
          this.$http.post('api/postcomment/'+this.$route.params.id,
          {content:this.msg.trim()}
          ).then(function(result){
              if(result.body.status === 0){
                  var cmt ={
                      user_name:'匿名用户',
                      add_time: Date.now(),
                      content:this.msg.trim(),
                  };
                  this.comment.unshift(cmt);
                  this.msg = '';
              }else{
                  return Toast('发表评论失败')
              }
          });
      }
  },
  props:["id"]
}
</script>

<style lang="scss" scoped>
.comment-container{
    .cmt-list{
        margin-top: 10px;
        textarea {
            font-size:12px;
            margin:0;
        }
    }
    .cmt-title{
        font-size:12px;
        background-color: #ccc;
    }

    .cmt-body{
        font-size: 12px;
        text-indent: 2em;
        line-height: 35px;
    }
}
</style>

